package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Dict;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.V;
import com.by.query.DictQuery;
import com.by.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "字典管理")
@RequestMapping("/api")
public class DictController extends BaseController {

    @Autowired
    DictService dictService;

    @PostMapping("/dict")
    @ApiOperation("新增字典")
    public ResultBean save(@Validated @RequestBody Dict dict, BindingResult result) {
        V.validate(result);
        int rs = dictService.save(dict);
        return saveResult(rs);
    }

    @GetMapping("/dict")
    @ApiOperation("查询字典")
    public ResultBean get(@RequestParam String id) {
        Dict dict = dictService.get(id);
        return success("字典信息", dict);
    }

    @GetMapping("/dict/child/{pid}")
    @ApiOperation("根据父id查询字典列表")
    public ResultBean<List<Dict>> query(@PathVariable("pid") int pid) {
       List list = dictService.query(pid);
        return success("字典列表", list);
    }

    @ApiImplicitParam(name = "id", value = "字典ID", dataTypeClass = Integer.class, paramType = "query", required = true)
    @ApiOperation("删除字典")
    @DeleteMapping("/dict")
    public ResultBean delete(@RequestParam(required = true) int id) {
        int rs = dictService.delete(id);
        return deleteResult(rs);
    }
@PutMapping("/dict")
    @ApiOperation("修改字典")
    public ResultBean update(@RequestBody Dict dict){
        int rs = dictService.update(dict);
        return updateResult(rs);
    }
}
